﻿using System;
using System.Collections.Generic;
using System.Text;
using Android.Content.Res;
using Android.Runtime;
using Android.Text.Style;
using Android.Text;
using Android.OS;
using Android.Graphics;
using Java.Lang;
using Object = Java.Lang.Object;
using String = Java.Lang.String;

namespace LinkNote.Core
{
    class CheckBoxSpan : Object, ILeadingMarginSpan, IParcelableSpan
    {
        public static Resources resource;
        private int mGapWidth;
        private bool mWantColor;
        private Color mColor;

        private static int BULLET_RADIUS = 10;
        private static Path sBulletPath = null;
        public static int STANDARD_GAP_WIDTH = 2;

        public CheckBoxSpan() {
            mGapWidth = STANDARD_GAP_WIDTH;
            mWantColor = false;
            mColor = Color.Black;
        }

        public CheckBoxSpan(int gapWidth) {
            mGapWidth = gapWidth;
            mWantColor = false;
            mColor = Color.Black;
        }

        public CheckBoxSpan(int gapWidth, Color color) {
            mGapWidth = gapWidth;
            mWantColor = true;
            mColor = color;
        }

        public CheckBoxSpan(Parcel src) {
            mGapWidth = src.ReadInt();
            mWantColor = src.ReadInt() != 0;
            //mColor = src.ReadInt();
        }

        public void DrawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, ICharSequence text, int start, int end, bool first, Layout layout)
        {
            if (((ISpanned) text).GetSpanStart(this) == start) {
                Paint.Style style = p.GetStyle();
                Color oldcolor = Color.Black;

                if (mWantColor) {
                    oldcolor = p.Color;
                    p.Color = mColor;
                }

                p.SetStyle(Paint.Style.Fill);

                //if (c.isHardwareAccelerated()) {
                //    if (sBulletPath == null) {
                //        sBulletPath = new Path();
                //        // Bullet is slightly better to avoid aliasing artifacts on mdpi devices.
                //        sBulletPath.AddCircle(0.0f, 0.0f, 1.2f * BULLET_RADIUS, Path.Direction.Cw);
                //    }

                //    c.Save();
                //    c.Translate(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f);
                //    c.DrawPath(sBulletPath, p);
                //    c.Restore();
                //} else {
                    //c.DrawCircle(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f, BULLET_RADIUS, p);
                    //c.DrawRect(x, top, x + BULLET_RADIUS, bottom, p);
                    Bitmap bmp = BitmapFactory.DecodeResource(resource, Resource.Drawable.checkbox);
                    c.DrawBitmap(bmp, x, top, p);
                    //c.DrawPicture();
                    //c.DrawRoundRect(new RectF(x, top, x + BULLET_RADIUS, bottom), 5, 5, p);
                //}

                if (mWantColor) {
                    p.Color = oldcolor;
                }

                p.SetStyle(style);
            }
        }

        public int GetLeadingMargin(bool first)
        {
            return 2 * BULLET_RADIUS + mGapWidth;
        }

        public int SpanTypeId
        {
            get { return 8; }
        }

        public int DescribeContents()
        {
            return 0;
        }

        public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
        {
            dest.WriteInt(mGapWidth);
            dest.WriteInt(mWantColor ? 1 : 0);
            dest.WriteInt(mColor);
        }
    }
}
